package com.example.common.search;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;

import com.example.common.R;
import com.example.common.databinding.SearchRvItemBinding;
import com.example.common.network.IStartGameDetailActivity;

import org.jetbrains.annotations.NotNull;

public class SearchRAdapter extends RecyclerView.Adapter<SearchRAdapter.SearchRHolder> {
    SearchViewModel viewModel;
    public IStartGameDetailActivity iStartGameDetailActivity;

    public SearchRAdapter(SearchViewModel searchViewModel){
        viewModel=searchViewModel;
    }

    @Override
    public SearchRHolder onCreateViewHolder(@NonNull @NotNull ViewGroup parent, int viewType) {
        SearchRvItemBinding binding= DataBindingUtil.inflate(
                LayoutInflater.from(parent.getContext()),
                R.layout.search_rv_item,
                parent,
                false
        );

        SearchRHolder holder=new SearchRHolder(binding);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull @NotNull SearchRAdapter.SearchRHolder holder, int position) {
        holder.binding.setData(viewModel.getGames().get(position));
        //点击跳转游戏详情页面
        holder.binding.commonSearchRvItem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                iStartGameDetailActivity.openById(holder.binding.getData().getGame().getId());
            }
        });
    }

    @Override
    public int getItemCount() {
        if(viewModel.getGames()==null){
            return 0;
        }else {
            return viewModel.getGames().size();
        }
    }

    /***********************************************************************************************/
    class SearchRHolder extends RecyclerView.ViewHolder{
        private SearchRvItemBinding binding;

        public SearchRHolder(@NonNull @NotNull SearchRvItemBinding searchRvItemBinding) {
            super(searchRvItemBinding.getRoot());
            binding=searchRvItemBinding;
        }
    }
}
